﻿using CRMEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace CRMBiz.CalculateInterest
{
    public static class SimpleInterest
    {
        public static DataTable SimpleInterestPaymentScheduleByRepayment(decimal repaymentAmount, decimal loanAmount,
                                                                       int daysBetweenRepayments, DateTime settlementDate,
                                                                       DateTime firstPaymentDate,
                                                                       decimal annumInterestRate)
        {
            DataTable dtSchedule = new DataTable();
            dtSchedule.Columns.Add("DueDate", typeof(DateTime));
            dtSchedule.Columns.Add("Repayment", typeof(decimal));
            dtSchedule.Columns.Add("Principal", typeof(decimal));
            dtSchedule.Columns.Add("Interest", typeof(decimal));
            dtSchedule.Columns.Add("Balance", typeof(decimal));

            var repaymentList = new List<RepaymentDetail>();

            int daysFirstPeriod = (firstPaymentDate - settlementDate).Days;
            decimal dailyInterestRate = annumInterestRate / 365;
            decimal dailyInterestAmount = loanAmount * dailyInterestRate;

            decimal balance = loanAmount;
            decimal totalInterestAmount = 0;
            int i = 0;
            while (balance > 0)
            {
                DataRow dr = dtSchedule.NewRow();
                dr["DueDate"] = firstPaymentDate.AddDays(i * daysBetweenRepayments);
                dr["Repayment"] = repaymentAmount;
                decimal interestAmount;
                if (i == 0)
                {
                    interestAmount = dailyInterestAmount * daysFirstPeriod;
                }
                else
                {
                    interestAmount = dailyInterestAmount * daysBetweenRepayments;
                }
                dr["Interest"] = interestAmount;
                decimal principal = repaymentAmount - interestAmount;
                dr["Principal"] = principal;
                if (balance - principal > 0)
                {
                    balance = balance - principal;
                }
                else
                {
                    principal = balance;
                    dr["Principal"] = principal;
                    dr["Repayment"] = principal + interestAmount;
                    balance = 0;
                }
                dr["Balance"] = balance;
                totalInterestAmount += interestAmount;
                dtSchedule.Rows.Add(dr);

                repaymentList.Add(new RepaymentDetail { Date = Convert.ToDateTime(dr["DueDate"]), Amount = Convert.ToDecimal(dr["Repayment"]) });

                i++;
            }

            DataRow drTotal = dtSchedule.NewRow();
            drTotal["interest"] = totalInterestAmount;
            dtSchedule.Rows.Add(drTotal);

            return dtSchedule;
        }

        public static DataTable SimpleInterestPaymentScheduleByTerm(int term, decimal loanAmount, int daysBetweenRepayments,
                                                                DateTime settlementDate, DateTime firstPaymentDate,
                                                                decimal annumInterestRate)
        {
            DataTable dtSchedule = new DataTable();
            dtSchedule.Columns.Add("DueDate", typeof(DateTime));
            dtSchedule.Columns.Add("Repayment", typeof(decimal));
            dtSchedule.Columns.Add("Principal", typeof(decimal));
            dtSchedule.Columns.Add("Interest", typeof(decimal));
            dtSchedule.Columns.Add("Balance", typeof(decimal));

            var repaymentList = new List<RepaymentDetail>();

            int daysFirstPeriod = (firstPaymentDate - settlementDate).Days;
            decimal dailyInterestRate = annumInterestRate / 365;
            decimal dailyInterestAmount = loanAmount * dailyInterestRate;

            int totalDays = 0;

            if (daysFirstPeriod != 0)
                totalDays = daysFirstPeriod + ((term - 1) * daysBetweenRepayments);
            else
            {
                totalDays = daysFirstPeriod + (term * daysBetweenRepayments);
                daysFirstPeriod = daysBetweenRepayments;
            }

            decimal dailyPrincipal = loanAmount / totalDays;

            decimal balance = loanAmount;
            decimal totalInterestAmount = 0;

            for (int i = 0; i < term; i++)
            {
                DataRow dr = dtSchedule.NewRow();
                dr["DueDate"] = firstPaymentDate.AddDays(i * daysBetweenRepayments);

                decimal interestAmount;
                decimal principal;
                decimal repaymentAmount;
                if (i == 0)
                {
                    interestAmount = dailyInterestAmount * daysFirstPeriod;
                    principal = dailyPrincipal * daysFirstPeriod;
                    repaymentAmount = principal + interestAmount;
                }
                else
                {
                    interestAmount = dailyInterestAmount * daysBetweenRepayments;
                    principal = dailyPrincipal * daysBetweenRepayments;
                    repaymentAmount = principal + interestAmount;
                }
                dr["Interest"] = Math.Round(interestAmount, 2);
                dr["Principal"] = Math.Round(principal, 2);
                dr["Repayment"] = Math.Round(repaymentAmount, 2);
                if (balance - principal > 0)
                {
                    balance = balance - principal;
                }
                else
                {
                    principal = balance;
                    dr["Principal"] = Math.Round(principal, 2);
                    dr["Repayment"] = Math.Round((principal + interestAmount), 2);
                    balance = 0;
                }
                dr["Balance"] = Math.Round(balance, 2);
                totalInterestAmount += interestAmount;
                dtSchedule.Rows.Add(dr);

                repaymentList.Add(new RepaymentDetail { Date = Convert.ToDateTime(dr["DueDate"]), Amount = Convert.ToDecimal(dr["Repayment"]) });
            }

            DataRow drTotal = dtSchedule.NewRow();
            drTotal["interest"] = Math.Round(totalInterestAmount, 2);
            dtSchedule.Rows.Add(drTotal);
            return dtSchedule;
        }
    }
}
